package com.warmlight.voicepacket;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.data.UserCenterData;
import com.warmlight.voicepacket.data.eventbus.NotifyMyPlaylist;
import com.warmlight.voicepacket.dbdata.DBFloatPlaylist;
import com.warmlight.voicepacket.dbmanager.DBFloatPlayListDaoHelp;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpConfig;
import com.warmlight.voicepacket.http.ResponseData;
import com.warmlight.voicepacket.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;


/**
 * Created by jason on 2018/6/1.
 */

public class MyPlaylistEditActivity extends BaseActivity {

    //列表数据
    private DBFloatPlaylist mData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myplaylist_edit);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            if (bundle.containsKey("list_id")){
                DBFloatPlaylist listData = (DBFloatPlaylist) getIntent().getSerializableExtra("list_id");
                if(listData!=null){
                    mData = listData;
                }
                deleteLocal();
            }else if (bundle.containsKey("cover_net")){
                final UserCenterData.ListBean listBean = (UserCenterData.ListBean) bundle.getSerializable("cover_net");
                findViewById(R.id.tv_del_playlist).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteNet(listBean);
                    }
                });
            }
        }




        findViewById(R.id.iv_grey).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void deleteNet(final UserCenterData.ListBean listBean) {
        String user_del_audio = HttpConfig.USER_DEL_AUDIO;
        TreeMap<String,String> treeMap = new TreeMap<>();
        treeMap.put("auth_key", SPUtils.getLoginAuthKey());
        treeMap.put("type","1");
        treeMap.put("member_cover_id",listBean.getId());
        HttpClient.getInstance().postRxAndroidAES(user_del_audio,treeMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData responseData) {
                        if (TextUtils.equals("200",responseData.getCode())){
                            showToast("移除成功");
                            EventBus.getDefault().post(new NotifyMyPlaylist("net"));
//                            ShareCollectCallBackUtils.collectCallBack("0","-1",listBean.getId());
                            finish();
                        }else{
                            showToast(responseData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("删除失败，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    private void deleteLocal() {
        findViewById(R.id.tv_del_playlist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                DBHelper.getInstance(getThis()).delPlayList(mData.get_id());
                DBFloatPlayListDaoHelp.deleteById(getThis(),mData.getId());

                EventBus.getDefault().post(new NotifyMyPlaylist("notify"));
                showToast("移除成功");

                finish();
            }
        });
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
        overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out);
    }
}
